package com.education.mymediapalyer;

import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.SeekBar;
import android.widget.VideoView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button mBtnPlay;
    private MediaPlayer player;
    private SeekBar seekBar;
    private VideoView mVideoView;
    public static final int MEDIA_PLAY_TIME = 0X23;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MEDIA_PLAY_TIME:
                    if (mVideoView.isPlaying()) {
                        seekBar.setProgress(player.getCurrentPosition());
                        handler.sendEmptyMessageDelayed(MEDIA_PLAY_TIME, 1000);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtnPlay = (Button) findViewById(R.id.btn_player);
        seekBar = (SeekBar) findViewById(R.id.seek_time);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                player.seekTo(seekBar.getProgress());
            }
        });
        //创建播放的mediaplayer
        player = new MediaPlayer();
        mBtnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重置
                player.reset();
                try {
                    //设置播放的歌曲
                    player.setDataSource(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/tfboy.mp3");
                    //准备
                    player.prepare();
                    //播放完成的监听,用于播放下一首音频
                    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            //停止播放音乐,此时mediaplayer依然占用内存
                            player.stop();
                            //销毁mediaplayer
                            player.release();
                            //置空,取消掉引用能够被回收
                            player = null;
                            //手动调用垃圾回收
                            System.gc();
                        }
                    });
//                    设置准备完成的监听
                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
//                          准备完成时播放歌曲
                            player.start();
                            //得到音乐的毫秒数时长
                            int time = player.getDuration();
                            //将总时间设置为进度条的长度
                            seekBar.setMax(time);
                            handler.sendEmptyMessage(MEDIA_PLAY_TIME);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        mVideoView = (VideoView) findViewById(R.id.videoview);
        //设置播放的视频的路径
        mVideoView.setVideoPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/aa.mp4");

//        mVideoView.setVideoURI();
        mBtnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始播放视频
                mVideoView.start();
                mVideoView.setMediaController(new MediaController(MainActivity.this));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
